
Ext.define('Avrio.view.Device_Session', {
    extend: 'Ext.grid.Panel',

    alias: 'widget.device_session',

    title: 'Device Session Table',
    store: 'Device_Session',

    frame: true,
    columnLines: true,
    selType: 'cellmodel',
    xtype: 'cell-editing',

    initComponent: function() {
        var me = this;

        me.cellEditing = new Ext.grid.plugin.CellEditing({
            clicksToEdit: 1
        });

        Ext.applyIf(me, {
            height: 500,
            plugins: [me.cellEditing],
            viewConfig: { stripeRows: true },
// <editor-fold defaultstate="collapsed" desc="columns">
            columns: [
                {
                    xtype: 'gridcolumn', header: 'ID', dataIndex: 'id', width: 80, align: 'right', sortable: false, menuDisabled: true
                }, {
                    xtype: 'gridcolumn', header: 'Version', dataIndex: 'version', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield', allowBlank: false, minValue: 0 }
                }, {
                    xtype: 'gridcolumn', header: 'Type ID', dataIndex: 'device_type_id', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield', allowBlank: false, minValue: 0 }
                }, {
                    xtype: 'gridcolumn', header: 'Session ID', dataIndex: 'session_id', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield', allowBlank: false, minValue: 0 }
                }, {
                    xtype: 'gridcolumn', header: 'Status', dataIndex: 'device_status', width: 180, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Measurement Rate', dataIndex: 'measurement_sample_rate', width: 180, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield', allowBlank: false, minValue: 0 }
                }, {
                    xtype: 'gridcolumn', header: 'Facility ID', dataIndex: 'facility_id', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield', allowBlank: false, minValue: 0 }
                }, {
                    xtype: 'datecolumn', header: 'Start', dataIndex: 'device_start_time', renderer: Ext.util.Format.dateRenderer('m/d/Y'), width: 95, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'datefield', format: 'm/d/y', minValue: '01/01/1901', maxValue: Ext.Date.format(new Date(), 'm/d/Y') }
                }, {
                    xtype: 'datecolumn', header: 'Stop', dataIndex: 'device_stop_time', renderer: Ext.util.Format.dateRenderer('m/d/Y'), width: 95, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'datefield', format: 'm/d/y', minValue: '01/01/1901', maxValue: Ext.Date.format(new Date(), 'm/d/Y') }
                }, {
                    xtype: 'gridcolumn', header: 'Serial Number', dataIndex: 'device_serial_number', width: 180, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Inactivity Timeout', dataIndex: 'device_inactivity_timeout', width: 100, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield', allowBlank: false, minValue: 0 }
                }
            ],
// </editor-fold>
            dockedItems: [{
                xtype: 'pagingtoolbar',
                store: 'Device_Session',
                dock: 'bottom',
                displayInfo: true,
                items: [' ',{
                    text: 'New',
                    handler: function() {
                        me.cellEditing.cancelEdit();

                        var rec = Ext.create('Avrio.model.Device_Session',{
                            device_start_time: Ext.Date.clearTime(new Date()),
                            device_stop_time: null
                        });

                        var store = me.getView().getStore();
                        store.insert(0, rec);
                        
                        me.cellEditing.startEdit(0, 1); // 1 = Version
                    }
                }, {
                    text: 'Delete',
                    itemId: 'devicesession',
                    handler : function(grid, rowIndex) {
                        me.cellEditing.cancelEdit();

                        var store = me.getView().getStore();
                        var selection = me.getView().getSelectionModel().getSelection()[0];
                        if(selection) {
                            store.remove(selection);
                        }
                    },
                    disabled: true
                }]
            }],
            listeners: {
                render: function(grid) {
                    grid.getStore().load();
                },
                selectionchange: function(view, records) {
                    me.down('#devicesession').setDisabled(!records.length);
                }
            }
        });
        me.callParent(arguments);
    }
});
